$regfile = "m16def.dat"
$crystal = 8000000

'Inisialisasi variable Global
Dim Input_pinb As Integer , Icadc As Byte , Iccheck As Byte , Itemp As Long
Dim Winput_adc(6) As Word
Dim Wref_adc(6) As Word

'Deklarasi fungsi
Declare Sub Finisialisasi()

'Deklarasi vektor interupsi
On Timer0 Fint_handle_timer0

'Fungsi Utama
Fmain:
    Call Finisialisasi()
    Waitms 1000
    Do
    Loop
End

'Fungsi Interupsi Timer0
Fint_handle_timer0:
   Dim Ichannel As Integer

   Icadc = Icadc + 1
   Iccheck = Iccheck + 1
   If Icadc > 4 Then                                        '1 ms
       'Baca data input ADC
       For Ichannel = 0 To 5
           Winput_adc(ichannel + 1) = Getadc(ichannel)
           If Winput_adc(ichannel + 1) > Wref_adc(ichannel + 1) Then
               Itemp = 1
               Shift Itemp , Left , Ichannel
               Portc = Portc Or Itemp
           Else
               Itemp = 1
               Shift Itemp , Left , Ichannel
               Itemp = Not Itemp
               Portc = Portc And Itemp
           End If
       Next Ichannel
       Icadc = 0
   End If
   If Iccheck > 40 Then                                     '10 ms
       Input_pinb = Pinb And 1
       'Input_pinb = 0
       If Pinb.0 = 0 Then
           Portc = &H07
       End If
       Iccheck = 0
       Winput_adc(2) = Getadc(0)
       Cls
       Lcd Winput_adc(2)
   End If
Return

Sub Finisialisasi()
     'Inisialisasi Port
     Config Portc = Output
     Config Portd = Output
     Config Portb = Input
     '-Aktivasi internal pull up resistor pada PORTB
     Portb = &HFF

     'Inisialisasi LCD
     Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.2 , Rs = Portd.0
     Config Lcd = 16 * 2
     Initlcd
     Cls

     'Inisialisasi Timer0
     Config Timer0 = Timer , Prescale = 8

     'Inisialisasi ADC
     Config Adc = Single , Prescaler = Auto , Reference = Off

     Start Adc

     'Inisialisasi Interupsi
     Enable Interrupts
     '-inisialisasi Interupsi Timer0
     Enable Timer0
     Start Timer0

     'Inisialisasi variable awal
     Icadc = 0
     Iccheck = 0
     Wref_adc(1) = 500
     Wref_adc(2) = 500
     Wref_adc(3) = 500
     Wref_adc(4) = 500
     Wref_adc(5) = 500
     Wref_adc(6) = 500

End Sub

